//
//  SEDoctorDetailViewModel.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/13.
//

import UIKit

class SEDoctorDetailViewModel: SEBaseViewModel {
    var model: SEDoctorDetail = SEDoctorDetail()
    
    convenience init(model: SEDoctorDetail) {
        self.init()
        self.model = model
    }
    
    func setOnlineStatusLabel(_ label: UILabel) {
        label.text = model.isOnline.text
        label.textColor = model.isOnline.textColor
    }
    
    var inquiryCount: String {
        return "问诊量：\(model.count)"
    }
    
    var avatarPlaceholder: UIImage {
        model.sex == 1 ? Asset.iconDoctorAvatarMale.image : Asset.iconDoctorAvatarFemale.image
    }
    
    var goodAt: String { "擅长: \(model.speciality ?? " ")" }
    
    var jobAndDepart: String { "\(model.jobTitle ?? "")  \(model.deptName ?? "")" }

    var isHiddenLevel: Bool {
        guard let level = model.hospitalJb else {
            return true
        }
        return level.isEmpty
    }
    
    func inquiryCountLabelWidth(_ label: UILabel) -> CGFloat {
        return inquiryCount.width(height: label.height, font: label.font) + 7 * 2
    }

    func commentDetail(indexPath: IndexPath) -> SEDoctorCommentDetail {
        return model.comments.list[indexPath.row]
    }
    
    var jobTitleAndDepartment: String {
        let jobTitle = model.jobTitle ?? ""
        let department = model.deptName ?? ""
        if jobTitle.isEmpty || department.isEmpty {
            return jobTitle + department
        } else {
            return jobTitle + "/" + department
        }
    }

    var specialityDescribe: String {
        let speciality = model.speciality ?? ""
        return speciality.isEmpty ? speciality : "擅长: \(speciality)"
    }
    
    var serviceType: SEDoctorDetailServiceType = .online

    func numberOfRowsInSection(section: Int) -> Int {
        return section == 0 ? 1 : model.comments.list.count
    }

    var reserveReserveViewHeight: CGFloat {
        // 35: tableViewBgView top + bottom
        var height: CGFloat = 35
        for item in model.shifts {
            if item.isOpen {
                // 10: tableView top
                // 34: tableHeaderViewHieght
                // 10: tableFooterViewHeight
                // 16: collectionView top + bottom
                // 46: collectionViewCellHeight
                height = height + 10 + 34 + 10 + 16 + CGFloat(46 * item.shiftList.count)
            } else {
                height = height + 10 + 34 + 10 + 16 + CGFloat(46 * (item.shiftList.count > item.defalultLines ? item.defalultLines : item.shiftList.count))
            }
        }
        return height
    }
    
    var reserveCellHeight: CGFloat {
        switch serviceType {
        case .online: return 205
        case .reserve: return UITableView.automaticDimension
        }
    }

    func heigthForRow(indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0, indexPath.row == 0{
            return reserveCellHeight
        } else {
            return UITableView.automaticDimension
        }
    }

    var followedHandler: ((SEHttpStatus) -> Void)?

    /// 获取医生详情信息
    func getDoctorDetailInfo() {
        SEHttpManager.shared.request(SEDoctorAPI.doctorDetailInfo(id: model.id ?? ""), model: SEDoctorDetail.self, withLoading: true) { (model, httpStatus) in
            switch httpStatus {
            case .success:
                self.model = model ?? self.model
            case .auth(let message), .failure(let message):
                SEHUD.showError(message: message)
            }
            self.updateData?(httpStatus)
        }
    }

    /// 关注/取消关注医生
    func retquestFollowedDoctor() {
        SEHttpManager.shared.requestForAny(SEProfileAPI.followedDoctorOrHospital(userId: UserDefaults.userId, targetId: model.id ?? "", role: "2"), withLoading: true) { result, status in
            self.followedHandler?(status)
        }
    }
}
